{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 09\n",
    "# 定义、使用“列表统计量”类\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 创建 ListStatistics 类\n",
    "class ListStatistics:\n",
    "    # 构造函数，用于初始化属性\n",
    "    def __init__(self, data):\n",
    "        # ListStatistics包含一个data属性来存储浮点数列表\n",
    "        self.data = data\n",
    "\n",
    "    # 下面定义了4个方法\n",
    "    # 方法1：计算列表的长度，即元素的数量\n",
    "    def list_length(self):\n",
    "        return len(self.data)\n",
    "\n",
    "    # 方法2：计算列表元素之和\n",
    "    def list_sum(self):\n",
    "        return sum(self.data)\n",
    "\n",
    "    # 方法3：计算列表元素平均值\n",
    "    def list_mean(self):\n",
    "        return sum(self.data) / self.list_length()\n",
    "\n",
    "    # 方法4：计算列表元素方差\n",
    "    def list_variance(self, ddof=1):\n",
    "        # Delta自由度 ddof 默认为 1；无偏样本方差\n",
    "        sum_squares = sum((x_i - self.list_mean()) ** 2\n",
    "                          for x_i in self.data)\n",
    "        return sum_squares / (self.list_length() - ddof)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:11:47.522250Z",
     "end_time": "2024-07-08T21:11:47.540426Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# 创建一个浮点数列表\n",
    "data = [8.8, 1.8, 7.8, 3.8, 2.8, 5.6, 3.9, 6.9]\n",
    "# 创建ListStatistics对象实例\n",
    "float_list = ListStatistics(data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:12:04.931572Z",
     "end_time": "2024-07-08T21:12:04.946292Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表长度： 8\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表长度\n",
    "print(\"列表长度：\", float_list.list_length())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:12:13.706157Z",
     "end_time": "2024-07-08T21:12:13.728238Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表和： 41.4\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表和\n",
    "print(\"列表和：\", float_list.list_sum())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:12:24.172787Z",
     "end_time": "2024-07-08T21:12:24.212715Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表平均值： 5.175\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表平均值\n",
    "print(\"列表平均值：\", float_list.list_mean())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:12:34.225424Z",
     "end_time": "2024-07-08T21:12:34.278833Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表方差： 6.247857142857144\n",
      "列表方差 (ddof = 0)： 5.466875000000001\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表方差\n",
    "print(\"列表方差：\", float_list.list_variance())\n",
    "print(\"列表方差 (ddof = 0)：\", float_list.list_variance(0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:12:48.663411Z",
     "end_time": "2024-07-08T21:12:48.689985Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
